const express = require('express')

const router = express.Router()

const user = require('../router_handle/user')

const path = require('path')
const fs = require('fs')
// 导入使用验证规则模块
const expressJoi = require('@escook/express-joi')
// 导入验证规则对象
const userSchema = require('../schema/user')
const db = require('../db')

//下载学生开题报告文件
router.get('/download', (req, res) => {
    // console.log(req.query);
    db.query('select my_report from report where id=?', req.query.id, function (err, results) {
        // console.log(results);
        const filePath = path.join(__dirname, '../' + results[0].my_report)
        res.download(filePath)
    })
    // console.log(filePath);
})

//下载教师开题报告文件
router.get('/download_teacherReport', (req, res) => {
    // console.log(req.query);
    // res.setHeader('Content-Type', 'application/octet-stream;charset=utf8')
    db.query('select my_report from report where student_num=?', req.query.student_num, function (err, results) {
        // console.log(results);
        const filePath = path.join(__dirname, '../' + results[0].my_report)
        res.download(filePath)
    })

    // console.log(filePath);
})

// 下载任务书
router.get('/download_1', (req, res) => {
    db.query('select task from task where id=?', req.query.id, function (err, results) {
        const filePath = path.join(__dirname, '../' + results[0].task)
        res.download(filePath)
    })
})

// 学生下载论文
router.get('/download_paper', (req, res) => {
    // console.log(req.query);
    db.query('select article_location from article where id=?', req.query.id, function (err, results) {
        // console.log(results[0]);
        const filePath = path.join(__dirname, '../' + results[0].article_location)
        res.download(filePath)
    })
    // console.log(filePath);
})

// 教师下载论文
router.get('/teacher_download', (req, res) => {
    db.query('select article_location from article where id=?', req.query.id, function (err, results) {
        if (results[0].article_location) {
            // console.log(results[0]);
            const filePath = path.join(__dirname, '../' + results[0].article_location)
            res.download(filePath)
        } else {
            // @ts-ignore
            return
        }
    })

    // console.log(filePath);
})

// 注册
router.post('/regist', expressJoi(userSchema.user_schema), user.regist_user)

// 学生登录
router.post('/login', expressJoi(userSchema.user_schema), user.login_user)

//教师登录
router.post('/teacherLogin', expressJoi(userSchema.user_schema), user.login_teacher)

//系管理员登录
router.post('/minAdmin_login', expressJoi(userSchema.user_schema), user.login_minAdmin)


//学院管理员登录
router.post('/bigAdmin_login', expressJoi(userSchema.user_schema), user.login_bigAdmin)


module.exports = router